package UI;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;


/**
 * A wrapper object for some swing stuff
 * @author gldunn
 *
 */
public class ScrollTable {
	
	private DefaultTableModel model;
	//@ invariant model != null;
	private JTable table;
	//@ invariant table != null;
	private JScrollPane pane;
	//@ invariant pane != null;
	
	/** 
	 * This method creates an instance of Scroll Table
	 * @param columns: This contains the columns the ScrollTable will have.
	 */
	/*@
	 @ requires columns != null;	 
	 @ assignable model; 
	 @ assignable table; 
	 @ assignable pane; 
	 @*/
	public ScrollTable(Vector <String> columns) {
		
		// If necessary, create a blank column set
		if(columns == null) columns = new Vector<String>();
		
		// Create the data for the table
		model = new DefaultTableModel() {
			@Override
			public boolean isCellEditable(int row, int column) {
				// TODO Auto-generated method stub
				return false;
			}
		};
		table = new JTable(model);
		pane = new JScrollPane(table);
		
		// Add all the columns
		for(int i = 0; i < columns.size(); i++) {
			model.addColumn(columns.get(i));
		}
		
		// Set the selection mode
		table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
	}
	
	/**
	 * This method takes a result set and sets the scroll table equal to that result set. 
	 */
	/*@
	 @ requires set != null;
	 @ assignable model;
	 @*/
	public void updateTable(ResultSet set) {
		try {
			// First, delete the table contents
			while(model.getRowCount() > 0) { model.removeRow(0); }
			
			// Add all the input set to the table
			while(set.next()) {

				Vector<String> tmp = new Vector<String>();
				
				for(int i = 0; i < model.getColumnCount(); i++) {
					tmp.add( set.getString(i+1) );
				}
				model.addRow(tmp);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		pane.repaint();
	}
	
	/**
	 * This function removes the selected elements from the table.
	 */
	/*@
	 @ assignable model;
	 @*/
	public void removeSelected() {
		int[] row = table.getSelectedRows();
		if (row.length == 0) return;
		
		for(int i = row.length-1; i >= 0; i--) {
			System.out.println(model.getRowCount());
			model.removeRow(row[i]);
		}
	}
	
	/**
	 * This copies selected data from a ScrollTable to this scroll table.
	 */
	/*@
	 @ requires src != null;
	 @ assignable model;
	 @*/
	public void copyFrom(ScrollTable src) {

		DefaultTableModel srcmodel = src.getModel();
		JTable srctable = src.getTable();
		
		int row = srctable.getSelectedRow();
		if(row == -1) return;
		
		Vector<String> nRow = new Vector<String>();
		
		for(int i = 0; i < model.getColumnCount(); i++) {
			int srccol = srcmodel.findColumn(model.getColumnName(i));
			System.out.println("Column: " + srccol);
			System.out.println("Row: " + row);
			nRow.add((String) srcmodel.getValueAt(row, srccol));
		}
		
		model.addRow(nRow);
	}
	
	/**
	 * This method clears all entries from the table.
	 */
	/*@
	 @ assignable model;
	 @ ensures model.getRowCount() == 0;
	 @*/
	public void clear() {
		while(model.getRowCount() > 0) model.removeRow(0);
	}

	/**
	 * This method returns the pane of this object. Used primarily for attaching to windows. 
	 * @return The JScrollPane for this object.
	 */
	/*@
	 @ requires pane != null;
	 @ ensures /return == pane;
	 @*/
	public JScrollPane getPane() {
		return pane;
	}
	
	/**
	 * This method returns the table of this object.
	 * @return The JTable for this object.
	 */
	/*@
	 @ requires table != null;
	 @ ensures /return == table;
	 @*/
	public JTable getTable() {
		return table;
	}
	
	/**
	 * This method returns the model of this object.
	 * @return The DefaultTableModel for this object.
	 */
	/*@
	 @ requires model != null;
	 @ ensures /return == model;
	 @*/
	public DefaultTableModel getModel() {
		return model;
	}
}
